package com.zcpt.controller.user;

import com.alibaba.dubbo.config.annotation.Reference;
import com.zcpt.pojo.users.User;
import com.zcpt.realname.IUserService;
import io.swagger.annotations.Api;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;

@Api(tags = "用户注册接口")
@Controller
@RequestMapping("/user")
public class UserController {
    @Reference
    private IUserService userService;

    //添加用户
    @PostMapping("/addUser")
    public String addUser(User user) {
        System.out.println(user);
        if (user != null) {
            if (userService.queryfromUsername(user.getUsername())){
                //密码bcrypt加密
                String salt = BCrypt.gensalt();//29个字符  随机生成
                System.out.println(salt);
                String newPassword = BCrypt.hashpw(user.getPassword(), salt);
                user.setPassword(newPassword);
                //---------------------
                user.setCreated(new Date());
                userService.addUser(user);
                return "forward:/login.html";
            }else {
                System.out.println("用户名存在");
                return "forward:/fail.html";
            }
        }
        System.out.println("----------");
        return "forward:/fail.html";
    }

    //根据用户名查找用户id
    @PostMapping("/queryByUsername")
    public String queryByUsername(Model model, String username){
        System.out.println("queryByUsername方法执行了----"+username);
        User user = userService.queryByUsername(username);
        System.out.println(user);
        model.addAttribute("user", user);
        return "pay-address";
    }


}

